package com.onlineclass.classserver.controller;

import com.onlineclass.beas.Course;
import com.onlineclass.beas.ResultObject;
import com.onlineclass.beas.User;
import com.onlineclass.beas.UserInfo;
import com.onlineclass.classserver.service.IGetCourseinfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/course")
@Slf4j
public class CourseInfoController {


    @Autowired
    IGetCourseinfoService iGetCourseinfoService;

    @Autowired
    private RedisTemplate redisTemplate;

    private User getUser(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String token = "";
        for (Cookie cookie : cookies) {
            if ("token".equals(cookie.getName())){
                token = cookie.getValue();
            }
        }
        return  (User) redisTemplate.opsForValue().get(token);
    }

    @RequestMapping("/selectCourse")
    public ResultObject getCourseInfo(HttpServletRequest request){
        List<Course> list = iGetCourseinfoService.selectCourse(getUser(request));
        if (list != null) {
            return new ResultObject(1,"",list);
        } else {
            return new ResultObject(0,"查询不成功","");
        }

    }

}
